import { PositionHelper } from "utils/position/PositionHelper";
import { base64table } from "../move/Kuhn-Munkres";
import { MoveExtension } from "../move/MoveExtension";

 
 

export default class PowerCreepExtension extends PowerCreep
{
    private ismoveed?:boolean;
    private _nextPosCache?:RoomPosition;
    private moveLevel?:number;                  // 下一次移动的优先级
  /**
     * 三段式移动。
     * 1.第一段确定星门
     * 2.第二段确定本shard的房间route
     * 3.第三段确定两个相邻房间的移动path
    */

    /**
     * 重写移动.分段式移动。两种模式。1 站点重用模式(省cpu,只管走啥都不管)。 2. 危险检测模式(防挨揍)
     * @param goal 目标
     * @param opt
     */
    public mymoveTo(goal:{pos:RoomPosition,range:number,shard?:string},opt:IPathFinderOpt):CreepMoveReturnCode | ERR_NO_PATH | ERR_NOT_IN_RANGE | ERR_INVALID_TARGET
    {
         
        if(!this.memory.Debug)
        {
            this.memory.Debug={};
        }
        
        return MoveExtension.shardMoveTo(this as PowerCreep,goal,opt) as any;
    }
    public  move(direction: DirectionConstant | Creep,level:number=0): any
    {
        if(!this.room)
        {
            return ERR_NO_PATH
        }
        let tmpDir:DirectionConstant
        if(typeof direction =="number" )
        {
            tmpDir = direction;
        }
        else
        {
            tmpDir = this.pos.getDirectionTo(direction);
        }
        const pos = this.pos;
        const _move_intents = global._move_intents[this.room.name] ?? (global._move_intents[this.room.name] = {})
        const pos_str = base64table[pos.x] + base64table[pos.y];
        _move_intents[pos_str] = {id: this.id, step: [tmpDir],p:this.crossLevel()}
        return OK;
    }
    public flee( arg:IMoveGoal[], opt:IPathFinderOpt ): void
    {
        opt["PathCost"]={swampCost:1,plainCost:1};
        return MoveExtension.flee(this,arg,opt);
    }
    public printNextDirection()
    {
        
        {
            this.say('0');
        }
    }
    public nextPosition()
    {
        if(this._nextPosCache)
        {
            return this._nextPosCache
        }
        else 
        {
            const path = this.memory._move?.path;
            if(path)
            {
                const pathstep = Room.deserializePath(path);
                if(pathstep.length)
                {
                    this._nextPosCache = PositionHelper.Create({x: pathstep[0].x,y: pathstep[0].y,roomName: this.room!.name});
                    return this._nextPosCache
                }
            }
            this._nextPosCache = this.pos
        }
        
        return this.pos
    }
    ActiveRole():RoleName 
    {
    return '' as any;
    }
}
